import time
import json

'''
法院公告
'''


def get_court_report_info(mongo_instance, soup, cid, company):
    print('start 法院公告数据*******')
    for tr in soup.find('div', id='_container_court').find('tbody').find_all('tr') if soup.find('div',
                                                                                                id='_container_court') else []:
        item = {}
        tds = tr.find_all('td')
        # 详情json数据
        script = tds[6].find('script')
        if script is None:
            continue
        item = json.loads(script.text)
        item['date'] = tds[1].text
        if tds[2].find('a'):
            item['plaintiff'] = tds[2].find('a').text
            item['plaintiff_url'] = tds[2].find('a')['href']
        else:
            item['plaintiff'] = tds[2].find('span').text
        if tds[3].find('a'):
            item['defendant'] = tds[3].find('a').text
            item['defendant_url'] = tds[3].find('a')['href']
        else:
            item['defendant'] = tds[3].find('span').text
        item['type'] = tds[4].find('span').text
        item['court'] = tds[5].find('span').text

        item['company_id'] = cid
        item['company'] = company
        item['create_at'] = int(time.time())
        item['update_at'] = item['create_at']
        print(item)
        mongo_instance.court_report.update({'company_id': cid}, item, True)

    print('success 法院公告数据*******')
